﻿Imports BUS
Imports DTO
Public Class CapNhatHocSinh

    Public Sub CapNhatHS()
        dtgvdanhsachhscrt.Rows.Clear()
        Dim _Buscrt As New CHocSinhBus()
        Dim _danhsachhscrt As New List(Of CHocSinhCRT1)
        _danhsachhscrt = _Buscrt.LayDanhSachHSCrt()
        Dim i As Integer
        For i = 0 To _danhsachhscrt.Count - 1
            dtgvdanhsachhscrt.Rows.Add(i + 1, _danhsachhscrt(i).MaHS, _danhsachhscrt(i).HoTen, _danhsachhscrt(i).NgaySinh, _danhsachhscrt(i).Email, _danhsachhscrt(i).GioiTinh, _danhsachhscrt(i).DiaChi, _danhsachhscrt(i).TenLop, _danhsachhscrt(i).MaLop)
        Next
    End Sub

    Private Sub CapNhatHocSinh_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim _bus As New ClopBUS()
        cbblop.DataSource = _bus.LayDanhSachLop()
        cbblop.DisplayMember = "TenLop"
        cbblop.ValueMember = "MaLop"
        Dim _Bus1 As New CGioiTinhBUS()
        cbbgioitinh.DataSource = _Bus1.LayDanhSachGioiTinh()
        cbbgioitinh.DisplayMember = "TenGioiTinh"
        cbbgioitinh.ValueMember = "MaGioiTinh"
        CapNhatHS()
    End Sub

    Private Sub dtgvdanhsachhscrt_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtgvdanhsachhscrt.SelectionChanged

        If dtgvdanhsachhscrt.CurrentRow.Index >= 0 Then
            txthoten.Text = dtgvdanhsachhscrt.CurrentRow.Cells("clhoten").Value
            dtingaysinh.Value = dtgvdanhsachhscrt.CurrentRow.Cells("clngaysinh").Value
            txtemail.Text = dtgvdanhsachhscrt.CurrentRow.Cells("clemail").Value
            cbbgioitinh.Text = dtgvdanhsachhscrt.CurrentRow.Cells("clgioitinh").Value
            txtdiachi.Text = dtgvdanhsachhscrt.CurrentRow.Cells("cldiachi").Value
            cbblop.Text = dtgvdanhsachhscrt.CurrentRow.Cells("cllop").Value
        End If

    End Sub

    Private Sub btnthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        Me.Close()
    End Sub

    Private Sub btncapnhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncapnhat.Click
        Try
            Dim _HScrt1 As New CHocSinhCRT1()
            _HScrt1.MaHS = dtgvdanhsachhscrt.CurrentRow.Cells("clmahs").Value
            _HScrt1.HoTen = txthoten.Text
            _HScrt1.NgaySinh = dtingaysinh.Value
            _HScrt1.Email = txtemail.Text
            _HScrt1.GioiTinh = cbbgioitinh.Text
            _HScrt1.DiaChi = txtdiachi.Text
            _HScrt1.TenLop = cbblop.Text
            Dim _lopdto As New CLopDTO()
            _lopdto = cbblop.SelectedItem()
            _HScrt1.MaLop = _lopdto.MaLop
            Dim _bus As New CHocSinhBus()
            _bus.CapNhatHS(_HScrt1)
            Dim _bus1 As New CChiTietLopBUS()
            Dim _bussiso As New CChiTietLopBUS()
            Dim _buslop As New ClopBUS()
            Dim _lopdtodtgv As New CLopDTO()
            _lopdtodtgv.MaLop = dtgvdanhsachhscrt.CurrentRow.Cells("clmalop").Value

            If _lopdtodtgv.MaLop <> _HScrt1.MaLop Then
                If _bussiso.KiemTraSiSo(_lopdto) > 0 Then
                    If _bus1.CapNhatChiTietLop(_HScrt1) > 0 Then
                        _buslop.CapNhatSiSo(_lopdto)
                        _buslop.CapNhatSiSo(_lopdtodtgv)
                        MessageBox.Show("Cap Nhat Hoc Sinh Thanh Cong")
                    Else
                        MessageBox.Show("Cap Nhat Hoc Sinh That Bai")
                    End If
                    CapNhatHS()
                Else
                    MessageBox.Show("Lop " + _lopdto.TenLop + " da du Hoc Sinh")
                End If
            Else
                If _bus1.CapNhatChiTietLop(_HScrt1) > 0 Then
                    _buslop.CapNhatSiSo(_lopdto)
                    _buslop.CapNhatSiSo(_lopdtodtgv)
                    MessageBox.Show("Cap Nhat Hoc Sinh Thanh Cong")
                Else
                    MessageBox.Show("Cap Nhat Hoc Sinh That Bai")
                End If
                CapNhatHS()
            End If

            
            
        Catch ex As Exception
            MessageBox.Show("Loi --> " + ex.ToString())
        End Try
        
    End Sub
End Class